define(function (require, exports, module) {
    var myPub = require('./public');
    var layerMob = require('./layer_bindMob');

    var oWindow = $(window);                        // window
    var oContain = $('.js_contain');                // 大盒子
    var oHeader = $('.js_headerBox');               // 头部盒子
    var oFooter = $('.js_footer');                  // 页面底部
    var oGoTop = $('.js_goTop');                    // 回到顶部
    var oSideBox = $('.js_sideBox');		        // 侧边按钮框


    var oSildeNavScroll = $('.js_jrollNavScroll');          // 滚动区域
    var oSildeNavUl = $('.js_sildeNavUl');                  // 滚动区域列表
    var oActiveLi = oSildeNavUl.find('li');                 // 滚动区域的li
    var oCouponPage = $('.js_couponPage');                  // 页面几个页面容器
    var activeFixedBox = $('.js_couponNavFixed');           // 固定的导航容器
    var activeFixedLi = activeFixedBox.find('li');          // 固定的导航的li
    var oCouponCeaderBox = $('.js_couponCeaderBox');        // 整个导航的容器
    var oNavOpenHid = $('.js_navOpenHid');                  // 隐藏的导航
    var oNavOpenHidLi = oNavOpenHid.find('.couponHideLi');  // 隐藏导航区域的li
    var oNavOpenIcon = $('.js_navOpen');                    // 下拉图标
    var oCencouShadow = $('.js_cencouShadow');              // 阴影
    var bNavOff = true;                                     // 隐藏还是下拉
    var bGetCoupon = true;                                  // 防止立即领取多次点击
    var moreTapTime = null;                                 // 防止立即领取多次点击

    var oSildeNavJRoll = new myPub.ScrollBar();             // 导航滚动条
    var resizeTimer = null;

    // 回到过顶部
    oGoTop.on('click', function (e) {
        var oCouponArea = oCouponPage.find('.js_couponBoxDetail').eq(coAjax.couponNavNum);
        oCouponArea.stop().animate({scrollTop: '0'}, 250);
        oSideBox.hide().stop().animate({'right': '-3.5rem'}, 300);
        e.stopPropagation();
    });

    // 点击图标显示导航
    oNavOpenIcon.on('click',function (e) {
        var index = coAjax.couponNavNum;
        if(bNavOff){
            bNavOff = false;
            oNavOpenHid.show();
            oNavOpenHidLi.removeClass('active').eq(index).addClass('active');
            $(this).css({'border-color':'#ffffff'});
            oCencouShadow.show();
        } else{
            bNavOff = true;
            oNavOpenHid.hide();
            $(this).css({'border-color':'#f0f0f0'});
            oCencouShadow.hide();
        }
        e.stopPropagation();
    });

    // 点击阴影区域关闭阴影关闭下拉导航
    oCencouShadow.on('click',function (e) {
        bNavOff = true;
        oNavOpenHid.hide();
        oNavOpenIcon.css({'border-color':'#f0f0f0'});
        $(this).hide();
        e.stopPropagation();
    });

    // 点击下拉导航区域
    oNavOpenHid.on('click','.couponHideLi',function (e){
        var otargetNavHide = $(e.target);
        if(otargetNavHide.hasClass('couponHideLi')){
            var index = otargetNavHide.index();
            coAjax.couponNavNum = index;        //  设置历史记录
            coAjax.setSession();
            couponCenter.upDateShow(index,true);
            oNavOpenHidLi.removeClass('active').eq(index).addClass('active');
        }
        e.stopPropagation();
    });

    // 点击滚动导航展示对应的页面
    oCouponCeaderBox.on('click','li',function (e) {
        var otargetLi = e.target;
        if(otargetLi.nodeName.toLowerCase() == "li"){
            var index = $(otargetLi).index();
            coAjax.couponNavNum = index;        //  设置历史记录
            coAjax.setSession();
            couponCenter.upDateShow(index,true);
        }
        e.stopPropagation();
    });


    module.exports = {
        init: function (data, name) {
            couponCenter.InitPage();
            // connectHtml.cricleHandler(oCouponPage.find('.js_couponList').eq(0));
            couponCenter.setCouponNav();                            // 设置导航  有几个导航项就有几个页面
            coAjax.SetSildeNavScroll();
            coAjax.historyInit(function () {
                couponCenter.upDateShow(coAjax.couponNavNum,true);  // 每次更新时显示的页面
            });

            // 检测页面参数是否含有 startAliapp 跳转支付宝小程序
            myPub.gotoMiniAlipay();

            // 页面尺寸修改
            oWindow.on('resize', function () {
                if (resizeTimer) {
                    clearTimeout(resizeTimer)
                }
                resizeTimer = setTimeout(function () {
                    couponCenter.InitPage(true);
                    coAjax.SetSildeNavScroll();
                }, 400);

            });


            // 页面跳转记录数据
            oWindow.unload(function(){
                coAjax.setSession();
            });

        }
    };


    var couponGoodsUrl = "/coupon/findCouponByCenter";
    var loadSliderImgUrl = "/category/loadCategoryMaterialByName";         // 分类轮播图数据
    var coAjax = {
        bClickAdd:[],               // 点击加载
        couponNavNum: 0,            // 最新的导航索引
        bLoadOff:[],                // 页面是否加载数据
        startNum: [],               // 加载的开始
        limit: 20,
        scrollLoad:[],              // 滚动加载的开关
        couponScroolTop:[],         // 每个页面的滚动高度
        conCenterPagearr:[],              // 页码数
        imgSwiper:[],               // 轮播图数据图片
        imgSwiperUrl:[],            // 轮播图数据图片路径
        setSwiperTime:null,         // 定时器
        getData:[],                 // 数据
        allData:[],
        // 获取数据
        // 获取优惠券数据
        getDataList:function (index,coponNavId,isUpdate,fn){
            var _this = this;
            /*var nowStart = 0;
            var nowLimit = 0;
            if(isUpdate){
                nowStart = _this.startNum[index]*1;
                nowLimit = (_this.limit*1)*(_this.conCenterPagearr[index]*1);
            }else{
                nowStart = _this.limit*(_this.conCenterPagearr[index]-1)*1;
                nowLimit = _this.limit*1;
            }*/
            G_JSON_POST(couponGoodsUrl,{
                'type':coponNavId,
                'start':_this.startNum[index],
                'limit':_this.limit
            },function (data) {
                var oCouponDetailBox = oCouponPage.find('.js_couponBoxDetail').eq(index);
                var oCouponListBox = oCouponPage.find('.js_couponList').eq(index);
                var oCouponArea = oCouponPage.find('.js_couponArea').eq(index);
                if(data.status == 'success'){
                    _this.getData = data.coupons;
                    _this.allData = _this.allData.concat(_this.getData);
                    // console.log('coupons',index,_this.getData);
                    if(_this.getData != null){
                        if( _this.getData.length>0){
                            connectHtml.couponHtml(_this.getData,index);

                            //初始化那个圆形进度条
                            // couponCenter.circleInit();
                            couponCenter.getNow(index);
                            //如果请求回来的数据有没有比规定每次回来的数据少
                            if(_this.getData.length < _this.limit){
                                //少于规定数据就关闭数据加载的开关
                                _this.bLoadOff[index] = false;
                                //把加载中隐藏，显示没有更多
                                oCouponDetailBox.find('.js_loading').hide();
                                oCouponDetailBox.find('.js_loadNull').show();
                            }else {
                                //判断屏幕高度有没有少于优惠券容器的高度，
                                if(oWindow.height() > oCouponArea.height()){
                                    //如果容器高度少于屏幕的高度就再加载一次数据
                                    _this.startNum[index] = _this.limit*_this.conCenterPagearr[index];
                                    couponCenter.conCenterPagearr[index]++;
                                    _this.getDataList(index,coponNavId,false);
                                }
                                //显示加载中
                                oCouponDetailBox.find('.js_loading').show();
                            }
                        }
                        else if(_this.getData.length == 0 && _this.conCenterPagearr[index] > 1){
                            _this.bLoadOff[index]  = false;
                            //把加载中隐藏，显示没有更多
                            oCouponDetailBox.find('.js_loading').hide();
                            oCouponDetailBox.find('.js_loadNull').show();
                            return
                        }
                        else if(_this.getData.length == 0 && _this.conCenterPagearr[index] < 2){

                            oCouponDetailBox.find('.js_moreBtnBox').hide();
                            oCouponDetailBox.append(connectHtml.emptyHtml());
                            _this.bLoadOff[index]  = false;
                            oCouponDetailBox.find('.js_loading').hide();
                            return
                        }
                        else{
                            oCouponDetailBox.find('.js_moreBtnBox').hide();
                            oCouponDetailBox.append(connectHtml.emptyHtml());
                            _this.bLoadOff[index]  = false;
                            oCouponDetailBox.find('.js_loading').hide();
                            return
                        }
                    }
                    else{
                        oCouponDetailBox.find('.js_moreBtnBox').hide();
                        oCouponDetailBox.append(connectHtml.emptyHtml());
                        _this.bLoadOff[index]  = false;
                        oCouponDetailBox.find('.js_loading').hide();
                        return
                    }
                }else{
                    oCouponDetailBox.find('.js_moreBtnBox').hide();
                    oCouponDetailBox.append(connectHtml.emptyHtml());
                    _this.bLoadOff[index]  = false;
                    oCouponDetailBox.find('.js_loading').hide();
                    return
                }

                if(fn)fn()
            },function () {},function () {});
        },

        // 获取精选页面轮播图数据
        getSlideList:function (){
          var _this = this;
            G_JSON_GET(loadSliderImgUrl,{
                groupName:'领券中心轮播'
            },function (data) {
                if(data && data != null && data != undefined && data.length >0){
                    for(var i = 0;i < data.length ;i++){
                        var imageUrl = data[i].imageUrl;
                        if(imageUrl != null && imageUrl != '' && imageUrl != undefined){
                            _this.imgSwiper.push(imageUrl);
                            _this.imgSwiperUrl.push(data[i].url);
                        }
                    }
                    if(_this.imgSwiper.length > 0){
                        _this.swiperSlide();
                    }
                }
            }, function () {
                // start
            }, function () {
                // end
            })
        },

        // 轮播判断
        swiperSlide:function () {
            var _this = this;
            var oWrapperDetailPage =oCouponPage.find('.js_couponBoxDetail').eq(0);
            var oWrapperPage = oWrapperDetailPage.find('.js_couponSwiper');
            oWrapperPage.html(connectHtml.sliderHtml(_this.imgSwiper,_this.imgSwiperUrl));
            clearTimeout(_this.setSwiperTime);
            _this.setSwiperTime = setTimeout(function () {
                var oSwiperArea = oWrapperPage.find('.js_silderImg');   // 广告轮播框
                var oSwiperPagin = oWrapperPage.find('.js_pagin');      // 个数点
                _this.swiperAds(oSwiperArea,oSwiperPagin);
            }, 300);
        },

        // 广告轮播图轮播
        swiperAds:function (objArea,objPagin) {
            var indexSwiperAD = [];
            objArea.each(function (i) {
                if(indexSwiperAD[i] == undefined || indexSwiperAD[i] === '') {
                    if(objArea.eq(i).find('.swiper-slide').length < 2){
                        indexSwiperAD = objArea.eq(i).swiper({
                            pagination: objPagin.eq(i),
                            loop: false,
                            autoplay: 2000,
                            autoplayDisableOnInteraction : false,
                            lazyLoading : true
                        });
                        // 去掉轮播图下边的点
                        objPagin.removeClass('swiper-pagination-bullets')
                    }else {
                        indexSwiperAD = objArea.eq(i).swiper({
                            pagination: objPagin.eq(i),
                            loop: true,
                            autoplay: 2000,
                            autoplayDisableOnInteraction : false,
                            lazyLoading : true
                        });
                    }
                }
                else {
                    indexSwiperAD[i].onResize()
                }
            })
        },

        // 设置导航宽度和滚动
        SetSildeNavScroll: function () {
            var nLiwidth = 0;
            var nUlwidth = 0;
            var oLiSlide = oSildeNavScroll.find('.couponLi');
            if(oLiSlide != undefined && oLiSlide.length >0){
                for(var i = 0;i < oLiSlide.length;i++){
                    nLiwidth = oLiSlide.eq(i).outerWidth(true)+1;
                    nUlwidth += nLiwidth;
                }
            }
            oSildeNavUl.css({'width':nUlwidth});

            if(oSildeNavJRoll.obj === undefined) {
                oSildeNavJRoll.AddScroll(oSildeNavScroll[0], {
                    scrollY: false,
                    scrollX: true
                });
            }
            else {
                oSildeNavJRoll.ReScroll()
            }
        },

        // 历史数据定位
        historyInit:function (fn) {
            var _this = this;
            _this.getSession();
            if(fn)fn()
        },

        // 设置历史数据
        setSession:function () {
            var _this = this;
            sessionStorage.setItem('couponNavNum',_this.couponNavNum);
            // sessionStorage.setItem('couponScroolTop',_this.couponScroolTop);
            // sessionStorage.setItem('conCenterPagearr',_this.conCenterPagearr);
        },

        // 记录历史数据
        getSession:function () {
            var _this = this;
            var couponNavNum = 0;
            // var couponScroolTop = [];
            // var conCenterPagearr = [];
            if(sessionStorage.getItem("couponNavNum") != null) {
                // 获取历史下标数据
                couponNavNum = sessionStorage.getItem("couponNavNum");
                // 获取历史加载页码数据
                // conCenterPagearr = sessionStorage.getItem("conCenterPagearr").split(',');
                // 获取历史下滚动高度据
                // couponScroolTop = sessionStorage.getItem("couponScroolTop").split(',');
                /*for(var i = 0;i < conCenterPagearr.length;i++){
                    // 获取历史下滚动高度据
                    // _this.couponScroolTop[i] = parseInt(couponScroolTop[i]);
                    // 获取历史加载页码数据
                    // _this.conCenterPagearr[i] = parseInt(conCenterPagearr[i]);
                }*/
            }
            // 设置历史下标数据
            _this.couponNavNum = parseInt(couponNavNum);
        }
    };

    //对象事件
    var couponCenter = {
        time_Scroll:null,
        circleWidth: 0,                 // 保存当前屏进度条大小尺寸
        GoTop_Scroll:null,              // 返回顶部定时器
        InitPage: function (bOff) {  //初始化事件
            if (bOff) {
                oContain.css({
                    'padding-top': oHeader.outerHeight(true),
                    'padding-bottom': oFooter.outerHeight(true)
                });
            }
            oSideBox.stop().animate({'right': '-3.5rem'}, 300);
            couponCenter.circleInit()
        },
        circleInit:function () {   //圆形进度条初始化
            var $screenW = oWindow.width();
            $screenW = $screenW >= 900 ? 900 : $screenW;
            couponCenter.circleWidth = parseInt(90 * $screenW /750);
        },

        // 点击立即领取
        getNow:function (index) {
            var oCouponList = oCouponPage.find('.js_couponList').eq(index);
            oCouponList.on('click','.js_couponCenter',function (e) {
                var otargetCou = $(e.target);
                if(otargetCou.hasClass('js_getStoreCoupon')){

                    var oCouponCenter = otargetCou.parents('.js_couponCenter');         // 当前优惠券
                    var supplierId = oCouponCenter.attr('data-ispalt');                 // 根据此数据模拟是否是商家券
                    var oSubheadTip = oCouponCenter.find('.js_subhead');                // 当前优惠券的副标题
                    var oBusCouUse = oCouponCenter.find('.js_busCouUse');               // 当优惠券的左边区域
                    var oBusRightCouUse = oCouponCenter.find('.js_getBusStoreCoupon');  // 当优惠券的右边区域
                    var oCircleChart = oCouponCenter.find('.js_circleChart');           // 当优惠券的圈
                    var navNum = oCouponCenter.index();                                 // 当前优惠券在当前页面的下标
                    var couponData = coAjax.allData;

                    var couponId = oCouponCenter.attr('data-couponid');
                    var platCouType = oCouponCenter.attr('data-limit');
                    var drawNum = oCircleChart.attr('data-drawnum') * 1;
                    var couponNum = oCircleChart.attr('data-couponnum') * 1;
                    if(bGetCoupon){
                        bGetCoupon = false;
                        G_JSON_GET('/coupon/drawSupplierCoupon',{couponId:couponId},function (data) {
                            var dataItem = couponData[navNum];
                            var subheadHtml = dataItem.couponExpireType == 0 ? myPub.FormatDateTime(new Date(dataItem.beginDate.time),"minDay",".") + '-' + myPub.FormatDateTime(new Date(dataItem.endDate.time),"minDay",".") : "领取成功后"+dataItem.validDay+"天有效";
                            var goodsImagePathArr = [];
                            var goodsSnArr = [];
                            var busGoods = dataItem.supplierGoods;      // 商家优惠券里边的三个商家信息
                            if(busGoods != "" && busGoods.length >0){
                                for(var o = 0;o < busGoods.length;o++){
                                    goodsImagePathArr.push(busGoods[o].showImage);
                                    goodsSnArr.push(busGoods[o].goodsSn);
                                }
                            }
                            var useLink = "";
                            if(dataItem.isHasCoupon == 0){
                                if(supplierId == '0'){
                                    if(dataItem.useLink && dataItem.useLink != ""){
                                        useLink = dataItem.useLink;
                                    }else{
                                        if(platCouType != '4' && platCouType != '5' && platCouType != '6' && platCouType != '0'){
                                            useLink = "/coupon/couponToUse?couponId=" + dataItem.id;
                                        }else{
                                            useLink = 'home';
                                        }
                                    }
                                }else{
                                    useLink = "/coupon/couponToUse?couponId=" + dataItem.id;
                                }
                            }

                            if(data.status == 'success'){

                                myPub.TipLayer(data.message);

                                if(supplierId == '0' ){ //平台券
                                    oSubheadTip.text(subheadHtml);
                                    otargetCou.text('去使用');
                                    otargetCou.attr('href',useLink);
                                    if(useLink == 'home'){
                                        otargetCou.attr('_href',useLink);
                                        myPub.JudgeAppHref(otargetCou);
                                    }
                                }else{      // 商家券
                                    if(goodsImagePathArr && goodsImagePathArr.length > 0){
                                        oBusCouUse.html(connectHtml.busCouponHtml(dataItem,goodsImagePathArr,goodsSnArr));
                                        oBusRightCouUse.html(connectHtml.busCouponRightHtml(useLink));
                                        // myPub.JudgeAppHref($('.js_circleBtn').eq(navNum));
                                    }else{
                                        otargetCou.text('去使用');
                                        otargetCou.attr('href','/coupon/couponToUse?couponId='+ couponId);
                                    }
                                }
                                otargetCou.removeClass('js_getStoreCoupon');
                                myPub.JudgeAppHref($('.js_imgHrefJump'));

                                if(data.message.indexOf('领取成功')>-1){

                                    drawNum = drawNum + 1;

                                    var percentage = Math.round(drawNum / couponNum * 100);
                                    oCouponCenter.find('.js_circleChart').attr("data-percentage",percentage);
                                    oCouponCenter.find('.js_circleChart').attr("data-drawnum",drawNum);
                                    oCouponCenter.find('.circle-chart').remove();
                                    oCouponCenter.find('.js_percentage').text(percentage+'%');
                                    oCouponCenter.find('.circleBox').circlechart(couponCenter.circleWidth);
                                }
                            }else {
                                //如果是用户没绑定手机号就跳转去绑定手机号的地方
                                if(data.message.indexOf('绑定手机号')>0){
                                    layerMob.loginLayer()
                                }else {
                                    myPub.TipLayer(data.message);

                                }
                            }
                        });
                    }
                    // bGetCoupon = true;
                    clearTimeout(moreTapTime);
                    moreTapTime = setTimeout(function () {
                        bGetCoupon = true;
                    }, 500);
                }else {
                   /* if(otargetCou.attr('_href') != undefined && otargetCou.attr('_href') != ""){
                        window.location.href= otargetCou.attr('_href');
                    }*/
                    /*if(otargetCou.attr('_href') != undefined && otargetCou.attr('_href') != ""){
                        window.location.href= otargetCou.attr('_href');
                    }*/
                }
                e.stopPropagation();
            })
        },

        // 显示回到顶部按钮
        ShowGoTop: function (scrollTop) {
            scrollTop > 300 ? oSideBox.show().stop().animate({'right': 0}, 300) : oSideBox.hide().stop().animate({'right': '-3.5rem'}, 300);
        },

        // 滚动加载更多
        couScrollDate:function (index) {
            var _this = this;
            var minScrHeight = 200;
            var oScroll = oCouponPage.find('.js_scroll').eq(index);
            var oCouponDetailBox = oCouponPage.find('.js_couponBoxDetail').eq(index);
            var coupongoryId = 0;
            if(index <= 1){
                coupongoryId = activeFixedLi.eq(index).attr("data-categoryid");
            }else{
                coupongoryId = oActiveLi.eq(index).attr("data-categoryid");
            }
            oScroll.on('scroll',function () {
                var afterScroll = $(this).scrollTop();
                // 滚动距离页面底部200像素，延迟250毫秒加载
                clearTimeout(_this.time_Scroll);
                _this.time_Scroll = setTimeout(function () {
                    coAjax.couponScroolTop[index] = afterScroll;
                    coAjax.setSession();
                    _this.ShowGoTop(coAjax.couponScroolTop[index]);
                    if((oCouponDetailBox[0].scrollHeight - afterScroll - oScroll.height()) < minScrHeight){
                        if(coAjax.bLoadOff[index]){
                            if(!coAjax.scrollLoad[index]){      // 当对应的数据为false时再加载数据
                                // 滚动底部页数增加 起始数据等于上次结束数据  新的结束等于页数*页码数
                                coAjax.startNum[index] = coAjax.limit*coAjax.conCenterPagearr[index];
                                coAjax.conCenterPagearr[index]++;
                                // console.log('coupongoryId',coupongoryId);
                                coAjax.getDataList(index,coupongoryId,false);
                            }
                        }
                    }
                },250)
            })
        },

        // 设置导航对应的页面
        setCouponNav:function () {
            var _this = this;
            var length = oSildeNavUl.find('li').length;
            var addTopHtml = "";
            var shtmlBox = "";

            // 加载按钮
            var sMoreBox =
                '<div class="moreBtnBox js_moreBtnBox" >'+
                '   <p class="loadingBtn js_loadingBtn js_loadNull" style="display: none;">' +
                '       <span>没有更多</span>' +
                '   </p>'+
                '   <p class="loadingBtn js_loadingBtn js_loading" style="display: none;">'+
                '       <span class="icon_loading iconStyle icon-spinner3 myRotateRefresh"></span>'+
                '       <span>加载中</span>'+
                '   </p>'+
                '</div>';

            for(var i = 0;i < length;i++){
                coAjax.bLoadOff.push(true);
                coAjax.startNum.push(0);
                coAjax.couponScroolTop.push(0);
                coAjax.conCenterPagearr.push(1);
                coAjax.bClickAdd.push(true);
                coAjax.scrollLoad.push(true);
                if(i == 0){
                    addTopHtml = "<div class='js_couponSwiper'></div>"
                } else if(i == 1){
                    // 平台券提示
                    addTopHtml =
                        '<div class="platCouponTip">'+
                        '   <p>由年丰大当家平台发行，可跨店铺、可跨品类使用</p>'+
                        '</div>';
                }else if(i == 2){
                    addTopHtml =
                        '<div class="platCouponTip">'+
                        '   <p>由商家发行，支持商家店铺内所有商品使用</p>'+
                        '</div>';
                }else if(i == 3){
                    addTopHtml =
                        '<div class="platCouponTip">'+
                        '   <p>由商家发行，支持商家店铺内指定部分商品使用</p>'+
                        '</div>';
                }else if(i == 4){
                    addTopHtml =
                        '<div class="platCouponTip">'+
                        '   <p>由商家发行，支持商家店铺内指定商品使用</p>'+
                        '</div>';
                }else{
                    addTopHtml = ""
                }

                shtmlBox += '<div class="couponBoxDetail js_couponBoxDetail js_scroll">'+
                        addTopHtml+
                    '   <div class="js_couponArea">'+
                    '       <div class="couponList js_couponList"></div>'+
                            sMoreBox+
                    '   </div>'+
                    '</div>';
            }
            // 放入容器中
            oCouponPage.append(shtmlBox);
        },

        // 每次更新时
        upDateShow:function (index,isUpDate) {
            var _this = this;
            var activeLeft = 0;     // 导航有缓存值时  滑动的距离
            var coupongoryId = 0;
            if(index <= 1){
                activeLeft = 0;
                oActiveLi.removeClass('active');
                activeFixedLi.removeClass('active').eq(index).addClass('active');
                coupongoryId = activeFixedLi.eq(index).attr("data-categoryid");
            }else{
                activeFixedLi.removeClass('active');
                oActiveLi.removeClass('active').eq(index).addClass('active');
                activeLeft = oActiveLi.outerWidth(true)*(index-3);
                coupongoryId = oActiveLi.eq(index).attr("data-categoryid");
            }
            if(coAjax.bClickAdd[index]){
                coAjax.bClickAdd[index] = false;
                if(coAjax.bLoadOff[index]){
                    coAjax.getDataList(index,coupongoryId,isUpDate);
                    couponCenter.couScrollDate(coAjax.couponNavNum);
                }
                if(index == 0){
                    coAjax.getSlideList();      // 精选获取轮播图
                }
            }

            coAjax.swiperSlide();       // 让轮播动起来

            oCouponPage.find('.js_couponBoxDetail').hide().eq(index).show();
            couponCenter.ShowGoTop(coAjax.couponScroolTop[index]);
            oSildeNavJRoll.ScrollTo(-activeLeft,0,100); // 导航条滑动

            coAjax.scrollLoad[index] = false;

            clearTimeout(_this.GoTop_Scroll);
            /*_this.GoTop_Scroll = setTimeout(function () {
                oCouponPage.find('.js_couponBoxDetail').eq(coAjax.couponNavNum).stop().animate(
                    {scrollTop: coAjax.couponScroolTop[coAjax.couponNavNum]*1},
                    250);
            },300)*/
        }
    };
    /*coAjax.getData += coAjax.getData;
    couponCenter.getNow(coAjax.couponNavNum,coAjax.getData);*/


    //拼接的html
    var connectHtml = {
        // 当没有数据时
        emptyHtml: function () {
            var shtml = '<div class="errPage VBox BoxCenter null_errPage errPage_noBg js_errPage Height100">' +
                '            <div class="errImgBox" style="height: 80%">' +
                '                <figure class="errImg errImgFig" style="width: 70%;">' +
                '                    <img src="/static_v4/image/wonderful/coupon-null.png" alt="">' +
                '                </figure>' +
                '                <p class="errTxt" style="font-size: 1.23rem;margin-top: 1rem;">列表还是空的</p>' +
                '            </div>' +
                '        </div>';

            return shtml;

        },

        // 优惠券拼接html
        couponHtml:function (data,index) {
            var _this = this;
            var shtml = "";
            var oCouponListBox = oCouponPage.find('.js_couponList').eq(index);
            for(var i = 0;i < data.length;i++){
                var dataItem = data[i];
                var subheadHtml = "";       // 显示优惠券副标题的地方
                var toUseHtml = "";         // 显示去使用还是立即领取
                var full = (dataItem.couponNum <= dataItem.drawNum) ? true : false;     // 当领取完时
                var getCouponClass = "";
                var useLink = "";
                var goodsImagePathArr = [];
                var goodsSnArr = [];
                var busGoods = dataItem.supplierGoods;      // 商家优惠券里边的三个商品信息
                var imgHtml = "";       // 头图
                var bPaltCoupon = null;// 模拟平台券与商家券
                var bIsReceive = false;                     // 此优惠券是否领取
                var receiveHtml = '';                       // a标签出的显示问题
                var platCouType = dataItem.limitType;       // 平台优惠券的类型(是否商家不可用、商品不可用、分类不可用)
                var couponNameHtml = '';

                // 平台券与商家券
                if(dataItem.supplierId == 0){
                    bPaltCoupon = true;     // 平台券
                    couponNameHtml ='<h5 class="singleLine" >'+dataItem.couponName+'</h5>';
                    if(dataItem.isHasCoupon == 1){
                        subheadHtml = dataItem.couponExpireType == 0 ? myPub.FormatDateTime(new Date(dataItem.beginDate.time),"minDay",".") + '-' + myPub.FormatDateTime(new Date(dataItem.endDate.time),"minDay",".") : "领取成功后"+dataItem.validDay+"天有效"
                    }else{
                        subheadHtml = ""
                    }
                }else{
                    bPaltCoupon = false;    // 商家券
                    couponNameHtml = '<h5 class="singleLine" >'+dataItem.supplierNickName+'</h5>';
                    subheadHtml = '此优惠券仅限'+dataItem.supplierNickName+'店铺商品使用';
                }

                if(dataItem.isHasCoupon == 0){
                    bIsReceive = false;
                    useLink = "";
                    getCouponClass = "js_getStoreCoupon";
                }else{
                    if(bPaltCoupon){
                        if(dataItem.useLink && dataItem.useLink != ""){
                            bIsReceive = false;
                            useLink = dataItem.useLink;
                        }else{
                            if(platCouType != '4' && platCouType != '5' && platCouType != '6' && platCouType != '0'){
                                bIsReceive = false;
                                useLink = "/coupon/couponToUse?couponId=" + dataItem.id;
                            }else{
                                bIsReceive = true;
                                useLink = 'home';
                            }
                        }
                    }else{
                        bIsReceive = false;
                        useLink = "/coupon/couponToUse?couponId=" + dataItem.id;
                    }
                    getCouponClass = "";
                }

                if(full && dataItem.isHasCoupon == 1){
                    toUseHtml = '去使用'
                }else if(!full && dataItem.isHasCoupon == 0){
                    toUseHtml = '立即领取'
                }else if(!full && dataItem.isHasCoupon == 1){
                    toUseHtml = '去使用'
                }



                // 商家优惠券里边的三个商品信息
                if(busGoods != "" && busGoods.length >0){
                    for(var o = 0;o < busGoods.length;o++){
                        goodsImagePathArr.push(busGoods[o].showImage);
                        goodsSnArr.push(busGoods[o].goodsSn);
                    }
                }

                if(bIsReceive){
                    receiveHtml = '<a class="circleBtn js_circleBtn '+getCouponClass+'" _href="'+useLink+'">'+toUseHtml+'</a>'
                }else{
                    if((dataItem.isHasCoupon == 1) || (dataItem.useLink && dataItem.useLink != "")){
                        receiveHtml = '<a class="circleBtn js_circleBtn '+getCouponClass+'" href="'+useLink+'">'+toUseHtml+'</a>'
                    }else{
                        receiveHtml = '<a class="circleBtn js_circleBtn '+getCouponClass+'" >'+toUseHtml+'</a>'
                    }
                }


                // 头图
                if(dataItem.showImage != undefined && dataItem.showImage != ""){
                    imgHtml = '<img src="'+HTTP_IMAGE_URL+dataItem.showImage+smallImgArg200+'" alt="">'
                }else{
                    imgHtml = '<img src="/'+_base+'static_v4/image/iconImg/couDefaultImg.png" alt="">'
                }
                // 左边的优惠券内容
                var couponleftHtml =
                '               <div class="couponInfoLeft HBox BoxCenter">'+
                '                   <div class="couponPrice">'+
                                        imgHtml+
                '                   </div>'+
                '               </div>'+
                '               <div class="couponInfoCenter Bflex1 height_100">'+
                '                   <div class="couponUseType">'+
                '                       <div class="coupon_tit">'+
                                            couponNameHtml+
                '                           <div class="tip_use" >'+
                '                               <p class="js_subhead">'+subheadHtml+'</p>'+
                '                           </div>'+
                '                       </div>'+
                '                       <div class="tip_useLimt Bflex1">'+
                '                           <span>￥</span><span class="coupon_price">'+dataItem.costPrice+'</span>'+
                '                           <span class="coupon_limit">满'+dataItem.needPrice+'元可用</span>'+
                '                       </div>'+
                '                   </div>'+
                '               </div>';

                // 右边的优惠券内容
                var couponRightHtml =
                '   <div class="circleBox circleChart js_circleChart" style="height: 3.9rem;"  data-percentage="'+(data[i].drawNum/data[i].couponNum*100)+'" data-drawNum="'+dataItem.drawNum+'" data-couponNum= "'+dataItem.couponNum+'" >'+
                '      <div class="circleText VBox BoxCenter">'+
                '          <p>已抢</p>'+
                '          <p class="js_percentage">'+Math.round(dataItem.drawNum/dataItem.couponNum*100)+'%</p>'+
                '      </div>'+
                '  </div>'+
                '  <div class="getStoreCouponbtn">'+
                    receiveHtml+
                '  </div>';


                if(dataItem.supplierId != 0 && dataItem.isHasCoupon == 1 && goodsImagePathArr.length > 0){
                    couponleftHtml = _this.busCouponHtml(dataItem,goodsImagePathArr,goodsSnArr);
                    couponRightHtml = _this.busCouponRightHtml(useLink);
                }

                shtml = '<div class="coupon couponCenter js_couponCenter" data-limit="'+dataItem.limitType+'" data-isPalt = "'+dataItem.supplierId+'" data-couponId="'+dataItem.id+'">'+
                        '   <div class="couponBox" >' +
                        '       <div class="couponInfo HBox">'+
                        '           <div class="js_busCouUse Bflex1 HBox">'+
                                        couponleftHtml+
                        '           </div>'+
                        '           <div class="couponInfoRight HBox BoxCenter">'+
                        '               <div class="getStoreCoupon js_getBusStoreCoupon">'+
                                            couponRightHtml+
                        '               </div>'+
                        '           </div>'+
                        '       </div>'+
                        '   </div>'+
                        '   <div class="gapBox gapBox_right">'+
                        '       <span class="gap gap_top"></span>'+
                        '       <span class="gap_line"></span>'+
                        '       <span class="gap gap_bot"></span>'+
                        '   </div>'+
                        '</div>';

                oCouponListBox.append(shtml);
                _this.cricleHandler( oCouponListBox.find('.js_couponCenter').last() );
                if(bIsReceive){
                    myPub.JudgeAppHref($('.js_circleBtn').eq(i));
                }
                myPub.JudgeAppHref($('.js_imgHrefJump'));
            }

            /*$(shtml).find('.circleChart').css({'height' : couponCenter.circleWidth + 'px'});
            $(shtml).find('.circleBox').circlechart(couponCenter.circleWidth);
            $(shtml).find('.circleBoxOver').circlechart(couponCenter.circleWidth);*/

        },

        // 圆圈
        cricleHandler:function (obj) {
            var oCricleBox = obj;
            oCricleBox.find('.js_circleChart')
                .css({'height' : couponCenter.circleWidth})
                .circlechart(couponCenter.circleWidth);

            // oCricleBox.each(function (i) {
                // var oCricleSell = oCricleBox.eq(i);
                // var oCricleSell = oCricleBox;
                // oCricleSell.find('.js_circleChart').css({'height' : couponCenter.circleWidth + 'px'});
                // 先清除有的svg，以防屏幕尺寸变的时候变形
                // oCricleSell.find('.circleBox .circle-chart').remove();
                // oCricleSell.find('.circleBoxOver .circle-chart').remove();
                // oCricleSell.find('.js_circleChart').circlechart(couponCenter.circleWidth);
            // });


        },

        // 轮播图拼接
        sliderHtml:function (imgageData,urlData) {
            var _this = this;
            var shtml = "";
            for(var i = 0;i < imgageData.length;i++){
                if(urlData[i] == undefined || urlData[i] == null || urlData[i] == ''){
                    urlData[i] = '#'
                }
                shtml +=
                    ' <div class="swiper-slide">'+
                    '    <a href="'+ urlData[i] + '">'+
                    '      <img class="swiper-lazy" data-src="' + HTTP_IMAGE_URL +imgageData[i]+'"  src="' + _base + '/static_v4/image/watermark_logo_hui.png" alt="" >'+
                    '    </a>'+
                    ' </div>'
            }

            var slideHtml =
                '<section class="">'+
                '   <div class="swiper-container js_silderImg js_viewImgBox">'+
                '       <div class="swiper-wrapper">'+
                            shtml+
                '       </div>'+
                '       <div class="swiper-pagination js_pagin"></div>'+
                '   </div>'+
                '</section>';

            return slideHtml
        },

        // 领取后的商家优惠券内部左边样式
        busCouponHtml:function (data,imgArr,urlArr) {
            var busImgUrl = "";
            for(var i = 0;i < imgArr.length;i++){
                busImgUrl+=
                    '<div class="couponImg">'+
                    '   <a class="js_imgHrefJump" _href="'+urlArr[i]+'">'+
                    '       <img src="'+HTTP_IMAGE_URL+imgArr[i]+smallImgArg200+'" alt="">'+
                    '   </a>'+
                    '</div>';
            }
            var busHtml =
                '<div class="couponBusUse Bflex1">'+
                '   <p class="couponBusUseTit tip_useLimt">'+
                '       <span class="couponName">'+data.supplierNickName+'店铺使用</span>'+
                '       <span class="coupon_doll">￥</span><span class="coupon_price">'+data.costPrice+'</span>'+
                '       <span class="coupon_limit">满'+data.needPrice+'元可用</span>'+
                '   </p>'+
                '   <div class="couponBusUseImg">'+
                        busImgUrl+
                '   </div>'+
                '</div>';

            return busHtml
        },

        // 领取后的商家优惠券内部右边样式
        busCouponRightHtml:function (url) {
            var couponRightHtml=
                '   <div class="coupon_touese">'+
                '       <img src="/'+_base+'static_v4/image/iconImg/seal_gotted.png" alt="">'+
                '   </div>'+
                '   <div class="getStoreCoupon">'+
                '       <div class="cricleNull"></div>'+
                '       <div class="getStoreCouponbtn">'+
                '           <a class="circleBtn js_circleBtn" href="'+url+'">去使用</a>'+
                '       </div>'+
                '   </div>';
            return couponRightHtml
        }
    }
});